
package restaurante.cih;


import java.awt.HeadlessException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import restaurante.cci.CtrlInterno;
import restaurante.cdp.Produto;
import restaurante.cdp.enuns.Tamanho;
import restaurante.cdp.enuns.TipoProduto;
import restaurante.util.Rest.tipo;

public class JanFormularioProduto extends javax.swing.JFrame {

    private static JanFormularioProduto instance;
    private CtrlInterno c;
    List<Produto> listProduto = new ArrayList<>();
    private static JanEventosProduto janEventosProduto;
    private tipo MODO;
    private Produto produto;
    

    public tipo getMODO() {
        return MODO;
    }

    public void setMODO(tipo MODO) {
        this.MODO = MODO;
    }

    public List<Produto> getListProduto() {
        return listProduto;
    }

    public void setListProduto(List<Produto> listProduto) {
        this.listProduto = listProduto;
    }
    
    /**
     * Creates new form JanFormularioEmpresas
     */
    /**
     * Obtém uma instância da aplicação. Implementação do padrão Singleton.
     */
    public static JanFormularioProduto getInstance() throws Exception{
        if(instance == null){
            instance = new JanFormularioProduto();
            janEventosProduto = JanEventosProduto.getInstance(); 
            
        }
        return instance;
    }
    
    private JanFormularioProduto() throws Exception {
        initComponents();
        this.setLocationRelativeTo(null);
        
        List<Tamanho> listTam = new ArrayList<>();
        listTam.add(Tamanho.P);
        listTam.add(Tamanho.M);
        listTam.add(Tamanho.G);
        listTam.add(Tamanho.NENHUM);
        List<TipoProduto> listProd = new ArrayList<>();
        listProd.add(TipoProduto.BEBIDA);
        listProd.add(TipoProduto.LANCHE);
        listProd.add(TipoProduto.PRATO);
        listProd.add(TipoProduto.SOBREMESSA);
        jComboBox3.setModel(new DefaultComboBoxModel(listTam.toArray()));
        jComboBox4.setModel(new DefaultComboBoxModel(listProd.toArray()));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel12 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel11 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jComboBox3 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jComboBox4 = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jTextField10 = new javax.swing.JTextField();

        jLabel3.setText("jLabel3");

        setTitle("Cadastro");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Produto");
        getContentPane().add(jLabel1, java.awt.BorderLayout.PAGE_START);

        jButton1.setText("Salvar");
        jButton1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel12.add(jButton1);

        jButton2.setText("Voltar");
        jButton2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jButton2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel12.add(jButton2);

        getContentPane().add(jPanel12, java.awt.BorderLayout.PAGE_END);

        jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jPanel11.setLayout(new java.awt.GridLayout(7, 2));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Nome: ");
        jPanel11.add(jLabel2);
        jPanel11.add(jTextField6);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("Custo:");
        jPanel11.add(jLabel4);
        jPanel11.add(jTextField1);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("Valor:");
        jPanel11.add(jLabel5);
        jPanel11.add(jTextField2);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel8.setText("Tamanho:");
        jPanel11.add(jLabel8);

        jPanel2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox3.setMinimumSize(new java.awt.Dimension(20, 20));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jComboBox3, 0, 119, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(0, 4, Short.MAX_VALUE)
                .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel11.add(jPanel2);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("Tipo:");
        jPanel11.add(jLabel6);

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox4.setMinimumSize(new java.awt.Dimension(20, 20));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jComboBox4, 0, 119, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(0, 4, Short.MAX_VALUE)
                .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel11.add(jPanel3);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("Tempo de Preparo:");
        jPanel11.add(jLabel7);

        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });
        jPanel11.add(jTextField3);

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel9.setText("Sabor:");
        jPanel11.add(jLabel9);

        jTextField10.setMinimumSize(new java.awt.Dimension(40, 20));
        jPanel11.add(jTextField10);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addComponent(jPanel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap(50, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jPanel11, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        List componentes = new ArrayList();
        componentes.add(this.jTextField6.getText());
        componentes.add(this.jTextField1.getText());
        componentes.add(this.jTextField2.getText());
        componentes.add(this.jComboBox3.getSelectedItem());
        componentes.add(this.jComboBox4.getSelectedItem());
        componentes.add(this.jTextField3.getText());
        componentes.add(this.jTextField10.getText());
        
        
            
        acaoSalvar(componentes);               
            
            
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        acaoVoltar();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        try {  
            c = CtrlInterno.getInstance();
            
        } catch (Exception ex) {
            Logger.getLogger(JanFormularioProduto.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formWindowOpened

    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3ActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        acaoVoltar();
    }//GEN-LAST:event_formWindowClosed
    
    private void formWindowDeactivated(java.awt.event.WindowEvent evt) {
        acaoVoltar();
    }

     
    
     
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton jButton1;
    public javax.swing.JButton jButton2;
    public javax.swing.JComboBox jComboBox3;
    public javax.swing.JComboBox jComboBox4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jTextField1;
    public javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    public javax.swing.JTextField jTextField6;
    // End of variables declaration//GEN-END:variables

    
    public void acaoIncluir() {
        arrumarTela(true);
        jButton1.setVisible(true);
        setVisible(true);
    }

    public void arrumarTela(boolean b) {

        jTextField6.setEnabled(b);
        jTextField1.setEnabled(b);
        jTextField2.setEnabled(b);
        jComboBox3.setEnabled(b);
        jComboBox4.setEnabled(b);
        jTextField3.setEnabled(b);
        jTextField10.setEnabled(b);

    }

    public void acaoSalvar(List componentes) {
        String msg = validaCampos();
        if(msg.equals(""))
        {
            if(validarTextosFormatados())
            {
                if (MODO != tipo.INCLUIR) {
                    c.alterarProduto(componentes, produto);
                } 
                else {

                    limparTela();

                    produto = c.incluirProduto(componentes);
                }
                setVisible(false);

                c.dadosProduto(janEventosProduto.jTable1.getModel(), listProduto.size());
            }
        }
        else
        {
            JOptionPane.showMessageDialog(this, msg, "Informe: ",JOptionPane.WARNING_MESSAGE);
        }
    }

    public void limparTela() {
        jTextField6.setText("");
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        jTextField10.setText("");
    }

   public void acaoVoltar() {
        limparTela();
        setVisible(false);
    }


    public void acaoAlterar(int linhaSelecionada) {
        Produto p = listProduto.get(linhaSelecionada);
        montarTela(p);
        produto = p;
        arrumarTela(true);
        jButton1.setVisible(true);
        setVisible(true);
    }

    public void montarTela(Produto p) {
        jTextField6.setText(p.getNome());
        jTextField1.setText(p.getCusto().toString());
        jTextField2.setText(p.getValor().toString());
        jComboBox3.setSelectedItem(p.getTam());
        jComboBox4.setSelectedItem(p.getTipo());
        jTextField3.setText(p.getTempoPreparo().toString());
        jTextField10.setText(p.getSabor());
    }

    public void acaoConsultar(int selectedRow) {
        produto = listProduto.get(selectedRow);
        montarTela(produto);
        arrumarTela(false);
        jButton1.setVisible(false);
        setVisible(true);
    }
    
    public String validaCampos()
    {
        String msg = "";
        if(jTextField6.getText().equals(""))
        {
            msg+= "-Nome\n";
        }
        if(jTextField1.getText().equals(""))
        {
            msg+= "-Custo\n";
        }
        if(jTextField2.getText().equals(""))
        {
            msg+= "-Valor\n";
        }
        if(jTextField3.getText().equals(""))
        {
            msg+= "-Tempo de Preparo\n";
        }
        if(jTextField10.getText().equals(""))
        {
            msg+= "-Sabor\n";
        }
        return msg;
    }
    
    public boolean validarTextosFormatados()
    {
        try
        {
            if(((Double.valueOf((String) jTextField1.getText()))<0))
            {
                JOptionPane.showMessageDialog(this, "Custo Inválido!", "Informe: ",JOptionPane.WARNING_MESSAGE);
                return false;
            }
        }
        catch(NumberFormatException ex)
        {
            JOptionPane.showMessageDialog(this, "Formato do custo Inválido!", "Informe: ",JOptionPane.WARNING_MESSAGE);
            return false;
        }
        try
        {
            if((Double.valueOf((String) jTextField2.getText()))<0)
            {
                JOptionPane.showMessageDialog(this, "Valor Inválido!", "Informe: ",JOptionPane.WARNING_MESSAGE);
                return false;
            }
        }
        catch(NumberFormatException ex)
        {
            JOptionPane.showMessageDialog(this, "Formato do valor Inválido!", "Informe: ",JOptionPane.WARNING_MESSAGE);
            return false;
        }
        try
        {
            if((Double.valueOf((String) jTextField3.getText()))<0)
            {
                JOptionPane.showMessageDialog(this, "Tempo de Preparo Inválido!", "Informe: ",JOptionPane.WARNING_MESSAGE);
                return false;
            }
        }
        catch(NumberFormatException ex)
        {
            JOptionPane.showMessageDialog(this, "Formato do tempo de preparo Inválido!", "Informe: ",JOptionPane.WARNING_MESSAGE);
            return false;
        }
         
        return true;
    }
}

            
